package com.collect.cameroon.zhiScene.zhiCollection.zhiMember.zhiAccount;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.collect.cameroon.R;
import com.collect.cameroon.zhiBase.BaseFragment;
import com.collect.cameroon.zhiBean.zhiReceive.ZHIRGroupUserEntity;
import com.collect.cameroon.zhiBean.zhiSend.ZHISendEntity;
import com.collect.cameroon.zhiGlobal.zhiAppSettings.ZHIProjectRouter;
import com.collect.cameroon.zhiHelper.ZHIMainProcessHelper;
import com.collect.cameroon.zhiScene.zhiLogin.ZHILoginReduce;
import com.collect.cameroon.zhiScene.zhiWidget.zhiDialog.ZHIDialogMessage;
import com.collect.cameroon.zhiScene.zhiWidget.zhiMenuView.ZHIMenuImageView;

import java.util.ArrayList;

import butterknife.BindView;

@SuppressLint("NonConstantResourceId")
public class ZHIAccountMemberFragment extends BaseFragment<ZHIAccountMemberPresenter> implements ZHIAccountMemberContract.View {

    @BindView(R.id.tv_user_name)
    TextView tv_user_name;

    @BindView(R.id.ll_logout)
    LinearLayout ll_logout;

    @BindView(R.id.imv_admin)
    ZHIMenuImageView imv_admin;

    @BindView(R.id.imv_script_lib)
    ZHIMenuImageView imv_script_lib;

    @BindView(R.id.imv_reset_password)
    ZHIMenuImageView imv_reset_password;

    @BindView(R.id.imv_check_in)
    ZHIMenuImageView imv_check_in;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_account_member;
    }

    @Override
    protected void initInjector() {
        mFragmentComponent.inject(this);
    }

    @Override
    protected void initView(View view) {
        initEvent();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onGetErrorInfo() {
        ZHIProjectRouter.toRegLoginV2();
    }

    @Override
    public void onGetGroupUsers(ArrayList<ZHIRGroupUserEntity> zhirGroupUserEntityArrayList) {
        ZHIProjectRouter.toAdmin();
    }

    private void initEvent() {
        try {

            ll_logout.setOnClickListener(v -> ZHIDialogMessage.showDialog(getActivity(),
                    getResources().getString(R.string.prompt),
                    getResources().getString(R.string.logout_confirm),
                    getResources().getString(R.string.cancel),
                    getResources().getString(R.string.confirm),
                    tag -> {
                        if (1 == tag) {
                            //退出
                            ZHIMainProcessHelper.getInstance().logout();
                            ZHIProjectRouter.toRegLoginV2();
                        }
                    }));

            tv_user_name.setText(ZHIMainProcessHelper.getInstance().getFullName());

            imv_admin.setCFOnMenuItemDidChangedListener(item -> {
                if (mPresenter != null) {
                    mPresenter.getGroupUsers(getActivity(), new ZHISendEntity());
                }
            });

            imv_script_lib.setCFOnMenuItemDidChangedListener(item -> ZHIProjectRouter.toScriptLibraryV2());

            imv_admin.setVisibility(View.GONE);

            imv_reset_password.setCFOnMenuItemDidChangedListener(item -> {
                ZHILoginReduce.mAccount = ZHIMainProcessHelper.getInstance().getFullName();

                Log.d(TAG, "initEvent: "+ZHILoginReduce.mAccount);
                Log.d(TAG, "initEvent: "+ZHIMainProcessHelper.getInstance().getFullName());

                ZHIProjectRouter.toForgotPassword();
            });

            imv_check_in.setCFOnMenuItemDidChangedListener(item -> ZHIProjectRouter.toCheckIn());

        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
